import defaultImg from '@/components/defaultImg/defaultImg.vue' //默认图片组件
import inquiryListCard from '@/components/acessory/inquiryListCard'
export default {
  data() {
    return {
      dataList: [
        {
          name: '全部',
          checked: true,
        },
        {
          name: '待报价',
          checked: false,
        },
        {
          name: '已报价',
          checked: false,
        },
        {
          name: '已过期',
          checked: false,
        },
        {
          name: '我的订单',
          checked: false,
        },
      ],
      currentTab: 0, //当前tab选项
      pageIndex: 1, // 当前页
      total: NaN, //总条数
      quote_Status: [-1, 10, 20, 30], //查询的订单状态
      list: [], //列表
      showimg: false, //默认为空显示图片
      storeShow: false, //店铺列表弹窗
      showText: false, //无更多数据
      showModal: false, //弹窗
      eventType: '', //弹窗确认事件
      modalText: '',
      modalBtnLeft: '取消',
      modalBtnRight: '确定上架',
      modalTitle: '取消询价',
      id: '',
      pageLoading: false, //初始化加载效果
    }
  },
  components: {
    inquiryListCard,
    defaultImg,
  },
  onLoad() {
    uni.$on('showModal', (data) => {
      //type del 删除 cancel 取消 reset 上架
      this.showModal = true
      this.eventType = data.type
      this.id = data.id
      if (data.type === 'del') {
        this.modalText = '删除询价后将无法恢复，是否确认删除?'
        this.modalBtnLeft = '我再想想'
        this.modalBtnRight = '确定删除'
        this.modalTitle = '删除询价'
      } else if (data.type === 'cancel') {
        this.modalText = '取消询价后，商家无法看到您的询件寻求，是否确认取消?'
        this.modalBtnLeft = '我再想想'
        this.modalBtnRight = '确定取消'
        this.modalTitle = '取消询价'
      } else if (data.type === 'reset') {
        this.modalText =
          '上架此询价信息后，有效期截止 为' + data.date + '日，是否确认上架?'
        this.modalBtnLeft = '取消'
        this.modalBtnRight = '确定上架'
        this.modalTitle = '上架'
      }
    })
  },
  onShow() {
    this.getList()
  },
  onBackPress() {
    uni.redirectTo({
      url: '/pages_findAccessory/accessoryIndex/accessoryIndex',
    })
    return true
  },
  methods: {
    //点击样式停留
    chooseSelector(index) {
      if (index === this.currentTab) {
        return
      }
      if (index == 4) {
        uni.navigateTo({
          url:
            '/pages_order/accessoryOrder/accessoryOrderList/accessoryOrderList?tab=0',
        })
      } else {
        for (var i = 0; i < this.dataList.length; i++) {
          this.dataList[i].checked = false
        }
        this.dataList[index].checked = true
        this.currentTab = index
        this.pageIndex = 1
        this.total = NaN
        this.showText = false
        this.list = []
        this.getList()
      }
    },

    //获取列表
    getList() {
      if (this.showText) {
        return
      }

      if (this.pageLoading) {
        uni.showLoading()
      }
      this.$api.accessory
        .getInquiryList({
          pageIndex: this.pageIndex,
          pageSize: 10,
          quote_Status: this.quote_Status[this.currentTab],
        })
        .then((res) => {
          this.total = res.data.total
          if (res.data.total === 0) {
            this.showimg = true
          } else if (this.pageIndex === 1) {
            this.showimg = false
            this.list = res.data.list
          } else {
            this.list = this.list.concat(res.data.list)
          }
          if (this.pageIndex * 10 >= this.total) {
            this.showText = true
          }
          this.pageIndex++
          if (this.pageLoading) {
            uni.hideLoading()
          } else {
            this.pageLoading = true
          }
        })
        .catch((e) => {
          console.log(e)
          if (this.pageLoading) {
            uni.hideLoading()
          } else {
            this.pageLoading = true
          }
        })
    },

    //取消
    modalCancel() {
      console.log(22)
      this.showModal = false
    },

    //确认
    async modalConfirme() {
      try {
        let res = ''
        if (this.eventType === 'del') {
          res = await this.$api.accessory.deleteCarPart({
            id: this.id,
          })
        } else if (this.eventType === 'cancel') {
          res = await this.$api.accessory.cancelCarPart({
            id: this.id,
            quote_Status: 0,
          })
        } else if (this.eventType === 'reset') {
          res = await this.$api.accessory.onTheShelfCarPart({
            id: this.id,
          })
        }
        uni.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 3000,
        })
        this.showModal = false
        this.pageIndex = 1
        this.total = NaN
        this.showText = false
        this.list = []
        this.getList()
      } catch (e) {
        console.log(e)
        this.showModal = false
      }
    },

    //跳转发布询价
    newInquiry() {
      uni.navigateTo({
        url: '/pages_findAccessory/selectCarModel/selectCarModel',
      })
    },
  },
}
